﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBIHelper.Helper;

namespace MSBIHelper.UI
{
    public partial class Frm_Executes : Form
    {
        private string sqlconnstr = string.Empty;
        private string olapconnstr = string.Empty;
        public Frm_Executes()
        {
            InitializeComponent();
            MidModule.EventSend += new MsgDlg(MidModule_EventSend);
        }

        private void MidModule_EventSend(object sender, object msg)
        {
            if (sender != null)
            {
                if (msg == null && (msg as Dictionary<string, string>) == null)
                    return;
                Dictionary<string, string> dict = msg as Dictionary<string, string>;
                if (dict.Keys.Contains("TYPE") && dict["TYPE"] == "SQL")
                {
                    if (sqlconnstr.Length == 0)
                    {
                        sqlconnstr = dict.Keys.Contains("ConnectionString") ? dict["ConnectionString"] : "";
                    }

                    tb_sql.Text = dict.Keys.Contains("SQL") ? dict["SQL"] : "";
                }
                else if (dict.Keys.Contains("TYPE") && dict["TYPE"] == "MDX")
                {
                    olapconnstr = dict.Keys.Contains("ConnectionString") ? dict["ConnectionString"] : "";
                    tb_mdx.Text = dict.Keys.Contains("MDX") ? dict["MDX"] : "";
                }
                else { MessageBox.Show("wrong parameter"); }
            }
        }

        //mdx
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (tb_mdx.Text.Trim().Length < 10) return;
                //remove the provider now just now
                StringBuilder sbnew = new StringBuilder();
                //foreach (var item in sqlconnstr.Split(';'))
                //{
                //    if (item.ToLower().IndexOf("provider") == -1)
                //    {
                //        sbnew.Append(item + ";");
                //    }
                //}
               // sqlconnstr = sbnew.ToString().TrimEnd(';');

                dgv_mdx.DataSource = DbHelper.ExecuteMdx(olapconnstr,tb_mdx.Text);

                Util.SetDgvColumnWidth(dgv_mdx);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //sql
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (tb_sql.Text.Trim().Length < 10) return;
                //remove the provider now just now
                StringBuilder sbnew = new StringBuilder();
                foreach (var item in sqlconnstr.Split(';'))
	            {
                    if (item.ToLower().IndexOf("provider") == -1)
                    {
                        sbnew.Append(item + ";");
                    }
	            }
                sqlconnstr = sbnew.ToString().TrimEnd(';');
                tb_dbconnstr.Text = sqlconnstr;
                DbHelper db = new DbHelper(sqlconnstr);
                dgv_sql.DataSource = db.ExecuteDataTable(tb_sql.Text);

                Util.SetDgvColumnWidth(dgv_sql);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                tb_dbconnstr.Visible = true;
                btn_updateconnstr.Visible = true;
               
            }
        }
        //compare data
        private void button3_Click(object sender, EventArgs e)
        {

        }

        //updae db connstr
        private void button4_Click(object sender, EventArgs e)
        {
            btn_updateconnstr.Visible = false;
            tb_dbconnstr.Visible = false;
            sqlconnstr = tb_dbconnstr.Text;

        }

    }
}
